import axiosInstance from './axios'

export const get = (url, params) => {
	return axiosInstance({
		url,
		method: 'get',
		params
	})
}
export const post = (url, data, headerType) => {
	return axiosInstance({
		url,
		method: 'post',
		data,
		headerType
	})
}
export const put = (url, data) => {
	return axiosInstance({
		url,
		method: 'put',
		data,
		headerType
	})
}
export const http = (type) => {
	switch (type) {
		case 'GET':
			return get
		case 'POST':
			return post
		case 'PUT':
			return put
		default:
			return get
	}
}
const prefix = 'javascript:'
// 对输入字符进行转义处理
export const translateStr = (target) => {
	if (typeof target === 'string') {
		if (target.startsWith(prefix)) {
			const funcStr = target.split(prefix)[1]
			let result
			try {
				result = new Function(`${funcStr}`)()
			} catch (error) {
				console.log(error)
				window['$message'].error('js内容解析有误！')
			}
			return result
		} else {
			return target
		}
	}
	for (const key in target) {
		if (Object.prototype.hasOwnProperty.call(target, key)) {
			const subTarget = (target[key](target)[key] = translateStr(subTarget))
		}
	}
	return target
}
